package com.baizhi.zjs.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import oracle.net.aso.d;

import org.apache.struts2.ServletActionContext;

import com.baizhi.zjs.entity.FUSerEntity;
import com.baizhi.zjs.service.FrontUserServiceImpl;
import com.baizhi.zjs.service.FrontUserServicee;
import com.baizhi.zjs.util.Md5Utils;
import com.baizhi.zjs.util.UUIDUtil;
import com.opensymphony.xwork2.ActionSupport;

public class FrontUserAction extends ActionSupport{
	private String addressId;
	private FUSerEntity fuser;
	private FUSerEntity fuser11;
	
	private List<FUSerEntity> fuserlist;

	//获取输入的邮箱验证码
	private String emCodea2;
	private String em;
	//注册账号时邮箱回显
	private String password;
	//确认密码
	private String password1;
	//验证码
	private String number;
	//购物时进入登录的标志
	private Integer deng;
	//回显字符串
	private String qqq;
	
	//注册页面的数据都回来补全后存入session
public String addFrontUser(){
	System.out.println(fuser);
	System.out.println(password1);
	System.out.println(number +"输入的验证码");
	HttpSession session = ServletActionContext.getRequest().getSession();
	Object attribute = session.getAttribute("securityCode");
	System.out.println(attribute +"生成的验证码");
	/*private Integer  status ;//状态
private Date regist_time;//注册时间
private String salt;//盐*/
	//==============接收到数据添加到实体类中，并将实体类补全，存入session中,跳转到验证邮箱页面
	
	if(attribute.equals(number)){
	FrontUserServiceImpl impl = new FrontUserServiceImpl();
	String one = impl.showOne(fuser.getEmail());
	//判断邮箱是否重复//邮箱不重复
	if("ok".equals(one)){
		System.out.println("/判断邮箱是否重复//邮箱不重复");
	//创建UUid,为id设值
	String uuid = UUIDUtil.getUuid();
	//创建MD5对象
	Md5Utils md5Utils = new Md5Utils();
	//创建盐值
	String salt =md5Utils. getSalt(8);
	//将盐值与密码混合生成md5字符串
	String md5Code =md5Utils.getMd5Code(fuser.getPassword()+salt);
	//------------------
	
	//为id赋值
	fuser.setId(uuid);
	//将MD5生成的字符串放入实体类中
	fuser.setPassword(md5Code);
	//状态设置为1
	fuser.setStatus(1);
	//创建时间设置为当前时间
	fuser.setRegist_time(new Date());
	//保存盐值
	fuser.setSalt(salt);

	//将数据存入session,先不将数据存入数据库，验证邮箱后存入数据库。
	//System.out.println(fuser+"111111111111111111111111======================");
	//FrontUserServiceImpl impl = new FrontUserServiceImpl();
	//impl.addFUser(fuser);
	session.setAttribute("fuser", fuser);
	//跳转到生成邮箱随机码的action
	return "addUser1";
	
	}else{
		System.out.println("重复");
		qqq="邮箱已经被注册过了";
		return "emError";
	}
	
	}
	qqq="验证码错误";
	return "emError";
}
	
	
	
	
	
	
//邮箱随机码的action
public String  emCodea1(){
	number = Md5Utils.getSalt(4);
	System.out.println(number);
	//将邮箱验证码转入session中
	HttpSession session = ServletActionContext.getRequest().getSession();
	session.setAttribute("emilCodea", number);
	
	//跳转到邮箱验证页面
	return "addUser2";
	
	//验证成功后跳转到用户信息添加到数据库页面
	
	
	
}

//邮箱的验证码验证
public String emCode2(){
	HttpSession session = ServletActionContext.getRequest().getSession();
	String attribute =(String)session.getAttribute("emilCodea");
	System.out.println("session中的验证码========="+attribute);
	//验证成功后跳转到用户信息添加到数据库页面
	System.out.println(emCodea2+"邮箱验证码验证action==============");
	//验证成功，跳转添加用户信息页面
	if(attribute.equals(emCodea2)){
		
		FrontUserServicee impl = new FrontUserServiceImpl();
		FUSerEntity attribute2 = (FUSerEntity)session.getAttribute("fuser");
		System.out.println("进入添加===========");
		System.out.println(attribute2);
		impl.addFUser(attribute2);
		return "addUser";
	}
	//不成功跳转到邮箱随机码的action，继续校验
	return "addUser1";
}


//登录
	public String Login(){
		FrontUserServicee impl = new FrontUserServiceImpl();
		System.out.println("action============"+fuser11);
		String login2 = impl.login(fuser11);
		HttpSession session = ServletActionContext.getRequest().getSession();
		Integer intr= (Integer)session.getAttribute("deng");
		
		System.out.println(deng+"                        action");
		
		//判断返回的参数
		System.out.println(fuser+"==================判断返回的参数");
		if("dongError".equals(login2)){
			qqq="账号已冻结";
			return "error";
		}
		if("passwordNull".equals(login2)){
			qqq="密码为空";
			return "error";
		}
		if("emailError".equals(login2)){
			qqq="邮箱输入错误";
			return "error";
		}
		if("Error".equals(login2)){
			qqq="密码错误";
			return "error";
		}
		if("ok".equals(login2)){
			
			// 判断用没有登录标志位，没有到提交订单页面让其调到首页   
			System.out.println("----------------------------------");
			if(intr==null){
				System.out.println("====================");
			return "ok";
			}else{//否则就是判断是不是在下订单时登录的
				//如果登录标志中有值，说明是在提交订单时跳转到登录页面的，所以点击登录后返回提交订单页面
				return "dengOk";
			}
			
		}
		return "error";
	}
	
	//判断邮箱是否被注册过
	public String showOne(){
		FrontUserServiceImpl impl = new FrontUserServiceImpl();
	System.out.println("邮箱验证action----------------");
		String one = impl.showOne(addressId);
		if("ok".equals(one)){
			password=addressId;
			return "emok";
		}else{
			qqq="邮箱已经被注册过了";
			return "emError";
		}
		
	}

	
	
	//================后台===
	public String showAllUser(){
		System.out.println("action====");
		FrontUserServiceImpl impl = new FrontUserServiceImpl();
		fuserlist = impl.showAllUser();
		return "sshowAlluser";
	}
	
	//修改用户状态
	public String updateuserw(){
		FrontUserServiceImpl impl = new FrontUserServiceImpl();
		impl.updateUser(em);
		return "updateuserww";
	}

public FUSerEntity getFuser() {
	return fuser;
}
public void setFuser(FUSerEntity fuser) {
	this.fuser = fuser;
}
public String getPassword1() {
	return password1;
}
public void setPassword1(String password1) {
	this.password1 = password1;
}
public String getNumber() {
	return number;
}
public void setNumber(String number) {
	this.number = number;
}

public String getPassword() {
	return password;
}

public void setPassword(String password) {
	this.password = password;
}
public Integer getDeng() {
	return deng;
}
public void setDeng(Integer deng) {
	this.deng = deng;
}
public String getQqq() {
	return qqq;
}
public void setQqq(String qqq) {
	this.qqq = qqq;
}
public String getAddressId() {
	return addressId;
}
public void setAddressId(String addressId) {
	this.addressId = addressId;
}
public String getEmCodea2() {
	return emCodea2;
}
public void setEmCodea2(String emCodea2) {
	this.emCodea2 = emCodea2;
}






public FUSerEntity getFuser11() {
	return fuser11;
}






public void setFuser11(FUSerEntity fuser11) {
	this.fuser11 = fuser11;
}






public List<FUSerEntity> getFuserlist() {
	return fuserlist;
}






public void setFuserlist(List<FUSerEntity> fuserlist) {
	this.fuserlist = fuserlist;
}






public String getEm() {
	return em;
}






public void setEm(String em) {
	this.em = em;
}





}
